package com.photoeditor.demo.ui.widget;

import android.animation.AnimatorListenerAdapter;
import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.Shader;
import android.util.AttributeSet;
import android.widget.TextView;

import com.common.base.ComponentContext;
import com.common.base.utils.DeviceUtils;
import com.photoeditor.demo.util.ColorShades;

/**
 * Created by winn on 17/9/20.
 */

public class LinearGradientTextView extends TextView {

    private int mStartColor;
    private int mEndColor;
    private int mEdgeColor;

    private Rect mClipRect;

    private ValueAnimator mValueAnimator;
    private LinearGradient mLinearGradient;

    private ColorShades mColorShades;

    private final static int EDGE_WIDTH = DeviceUtils.dip2px(ComponentContext.getContext(), 30);

    public LinearGradientTextView(Context context) {
        super(context);
        init();
    }

    public LinearGradientTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public LinearGradientTextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        mClipRect = new Rect(0, 0, 0, 0);
    }

    public void setColor(int startColor, int endColor, int edgeColor) {
        mStartColor = startColor;
        mEndColor = endColor;
        mEdgeColor = edgeColor;
        mColorShades = new ColorShades();
        mColorShades.setFromColor(mStartColor);
        mColorShades.setToColor(mEndColor);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
    }

    public void startColorAnimation(int during, AnimatorListenerAdapter animatorListenerAdapter) {
        if (mValueAnimator != null && mValueAnimator.isRunning()) {
            return;
        }
        mClipRect = new Rect();
        mValueAnimator = ValueAnimator.ofInt(0, getWidth());
        mValueAnimator.setDuration(during);
        mValueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                int right = (int) animation.getAnimatedValue();
                int edgeW = EDGE_WIDTH;
                if (right <= getWidth() - EDGE_WIDTH) {
                    right += EDGE_WIDTH;
                } else if (right > getWidth() - EDGE_WIDTH) {
                    edgeW = getWidth() - right;
                    right = getWidth();
                }
                mClipRect.set(0, 0, right, getHeight());
                float delta = (mClipRect.right - edgeW) / (float) mClipRect.right;
                mColorShades.setShade((float) right / getWidth());
                int endColor = mColorShades.generate();
                mLinearGradient = new LinearGradient(0, getHeight(), mClipRect.right, getHeight(),
                        new int[]{mStartColor, endColor, mEdgeColor},
                        new float[]{0, delta, 1}, Shader.TileMode.CLAMP);
                invalidate();
            }
        });
        if (animatorListenerAdapter != null) {
            mValueAnimator.addListener(animatorListenerAdapter);
        }
        mValueAnimator.start();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (mValueAnimator != null) {
            canvas.clipRect(mClipRect);
            Paint paint = getPaint();
            paint.setShader(mLinearGradient);
            super.onDraw(canvas);
            paint.setShader(null);
        }
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        if (mValueAnimator != null && mValueAnimator.isRunning()) {
            mValueAnimator.cancel();
        }
    }
}
